home *** CD-ROM | disk | FTP | other *** search
- #include <stdio.h>
- #include <ctype.h>
- #include "arc.h"
- #ifdef MSDOS
- #include <dir.h>
- #endif
- #ifdef BSD
- #include <sys/types.h>
- #include <sys/dir.h>
- #endif
-
- /* Miscellaneous routines to get ARC running on BSD 4.2... */
-
-
- char *
- setmem(dest, size, c)
- char *dest;
- unsigned INT size;
- char c;
- {
- unsigned INT i;
-
- for (i = 0; i < size; dest[i] = c, i++);
- return (&dest[0]);
- }
-
- #ifdef MTS
- #define CUTOFF sepchr[0]
- #endif
- #ifdef MSDOS
- #define CUTOFF '\\'
- #endif
- #ifdef BSD
- #define CUTOFF '/'
- #endif
-
- static INT
- _makefn(source, dest)
- unsigned char *source;
- unsigned char *dest;
- {
- INT j;
-
- setmem(dest, 17, 0); /* clear result field */
- for (j = 0; *source && *source != '.'; ++source)
- if (j < 8)
- dest[j++] = *source;
- for (j = 9; *source; ++source)
- if (j < 13)
- dest[j++] = *source;
- }
- /*
- * make a file name using a template
- */
-
- char *
- makefnam(rawfn, template, result)
- unsigned char *rawfn; /* the original file name */
- unsigned char *template; /* the template data */
- unsigned char *result; /* where to place the result */
- {
- unsigned char et[17], er[17], rawbuf[100], *i, *rindex();
-
- *rawbuf = 0;
- strcpy(rawbuf, rawfn);
- #ifdef MTS
- i = rawbuf;
- if (rawbuf[0] == tmpchr[0]) {
- i++;
- strcpy(rawfn, i);
- } else
- #endif
- if ((i = rindex(rawbuf, CUTOFF))) {
- i++;
- strcpy(rawfn, i);
- }
- #ifdef MSDOS
- else if ((i = rindex(rawbuf, ':'))) {
- i++;
- strcpy(rawfn, i);
- }
- #endif
- if (i)
- *i = 0;
- else
- *rawbuf = 0;
-
- _makefn(template, et);
- _makefn(rawfn, er);
- *result = 0; /* assure no data */
- strcat(result, rawbuf);
- strcat(result, er[0] ? er : et);
- strcat(result, er[9] ? er + 9 : et + 9);
- return ((char *) &result[0]);
- }
-
- #if MSDOS
-
- INT
- alphasort(dirptr1, dirptr2)
- struct direct **dirptr1, **dirptr2;
- {
- return (strcmp((*dirptr1)->d_name, (*dirptr2)->d_name));
- }
-
- #endif
-
- INT
- upper(string)
- char *string;
- {
- char *p;
-
- for (p = string; *p != NULL; p++)
- if (islower(*p))
- *p = toupper(*p);
- }
- INT
- arc_abort(s, arg1, arg2, arg3)
- char *s;
- {
- fprintf(stderr, "ARC: ");
- fprintf(stderr, s, arg1, arg2, arg3);
- fprintf(stderr, "\n");
- #ifdef BSD
- perror("BSD");
- #endif
- exit(1);
- }
-
- #ifndef MTS
-
- char *
- gcdir(dirname)
- char *dirname;
-
- {
- if (dirname == NULL || strlen(dirname) == 0)
- dirname = (char *) malloc(1024);
-
- getwd(dirname);
- }
-
- char *pattern; /* global so that fmatch can use them */
- INT filemode;
-
- char *
- dir(filename, mode) /* get files, one by one */
- char *filename; /* template, or NULL */
- INT mode; /* search mode bits */
- {
- static struct direct **namelist;
- static char **NameList;
- #ifdef BSD
- int alphasort();
- int scandir();
- #endif /* BSD */
- int fmatch();
- static INT Nnum = 0, ii;
- char *result = NULL;
-
- pattern = filename;
- filemode = mode; /* set up globals for fmatch */
- if (Nnum == 0) { /* first call */
- Nnum = scandir(".", &namelist, fmatch, alphasort);
- NameList = (char **) malloc(Nnum * sizeof(char *));
- for (ii = 0; ii < Nnum; ii++) {
- (NameList)[ii] = (char *) malloc(namelist[ii]->d_namlen + 1);
- strcpy((NameList)[ii], namelist[ii]->d_name);
- }
- ii = 0;
- }
- if (ii >= Nnum) { /* all out of files */
- if (Nnum) { /* there were some files found */
- for (ii = 0; ii < Nnum; ii++)
- free(namelist[ii]);
- free(namelist);
- }
- Nnum = 0;
- return (NULL);
- } else {
- return ((NameList)[ii++]);
- }
- }
-
-
- #define ASTERISK '*' /* The '*' metacharacter */
- #define QUESTION '?' /* The '?' metacharacter */
- #define LEFT_BRACKET '[' /* The '[' metacharacter */
- #define RIGHT_BRACKET ']' /* The ']' metacharacter */
-
- #define IS_OCTAL(ch) (ch >= '0' && ch <= '7')
-
- typedef INT BOOLEAN;
- #define VOID short
- #define TRUE 1
- #define FALSE 0
- #define EOS '\000'
-
- static BOOLEAN do_list();
- static char nextch();
- static VOID list_parse();
-
-
-
- /*
- * FUNCTION
- *
- * do_list process a list and following substring
- *
- * SYNOPSIS
- *
- * static BOOLEAN do_list (string, pattern) register char *string; register char
- * *pattern;
- *
- * DESCRIPTION
- *
- * Called when a list is found in the pattern. Returns TRUE if the current
- * character matches the list and the remaining substring matches the
- * remaining pattern.
- *
- * Returns FALSE if either the current character fails to match the list or the
- * list matches but the remaining substring and subpattern's don't.
- *
- * RESTRICTIONS
- *
- * The mechanism used to match characters in an inclusive pair (I.E. [a-d]) may
- * not be portable to machines in which the native character set is not
- * ASCII.
- *
- * The rules implemented here are:
- *
- * (1) The backslash character may be used to quote any special character.
- * I.E. "\]" and "\-" anywhere in list, or "\!" at start of list.
- *
- * (2) The sequence \nnn becomes the character given by nnn (in octal).
- *
- * (3) Any non-escaped ']' marks the end of list.
- *
- * (4) A list beginning with the special character '!' matches any character
- * NOT in list. The '!' character is only special if it is the first
- * character in the list.
- *
- */
-
-
-
- /*
- * PSEUDO CODE
- *
- * Begin do_list Default result is no match Skip over the opening left bracket
- * If the next pattern character is a '!' then List match gives FALSE Skip
- * over the '!' character Else List match gives TRUE End if While not at
- * closing bracket or EOS Get lower and upper bounds If character in bounds
- * then Result is same as sense flag. Skip over rest of list End if End while
- * If match found then If not at end of pattern then Call match with rest of
- * pattern End if End if Return match result End do_list
- *
- */
-
- static BOOLEAN
- do_list(string, pattern)
- register char *string;
- char *pattern;
- {
- register BOOLEAN ismatch;
- register BOOLEAN if_found;
- register BOOLEAN if_not_found;
- auto char lower;
- auto char upper;
-
- pattern++;
- if (*pattern == '!') {
- if_found = FALSE;
- if_not_found = TRUE;
- pattern++;
- } else {
- if_found = TRUE;
- if_not_found = FALSE;
- }
- ismatch = if_not_found;
- while (*pattern != ']' && *pattern != EOS) {
- list_parse(&pattern, &lower, &upper);
- if (*string >= lower && *string <= upper) {
- ismatch = if_found;
- while (*pattern != ']' && *pattern != EOS) {
- pattern++;
- }
- }
- }
- if (*pattern++ != ']') {
- fprintf(stderr, "warning - character class error\n");
- } else {
- if (ismatch) {
- ismatch = match(++string, pattern);
- }
- }
- return (ismatch);
- }
-
-
-
- /*
- * FUNCTION
- *
- * list_parse parse part of list into lower and upper bounds
- *
- * SYNOPSIS
- *
- * static VOID list_parse (patp, lowp, highp) char **patp; char *lowp; char
- * *highp;
- *
- * DESCRIPTION
- *
- * Given pointer to a pattern pointer (patp), pointer to a place to store lower
- * bound (lowp), and pointer to a place to store upper bound (highp), parses
- * part of the list, updating the pattern pointer in the process.
- *
- * For list characters which are not part of a range, the lower and upper bounds
- * are set to that character.
- *
- */
-
- static VOID
- list_parse(patp, lowp, highp)
- char **patp;
- char *lowp;
- char *highp;
- {
- *lowp = nextch(patp);
- if (**patp == '-') {
- (*patp)++;
- *highp = nextch(patp);
- } else {
- *highp = *lowp;
- }
- }
-
-
-
- /*
- * FUNCTION
- *
- * nextch determine next character in a pattern
- *
- * SYNOPSIS
- *
- * static char nextch (patp) char **patp;
- *
- * DESCRIPTION
- *
- * Given pointer to a pointer to a pattern, uses the pattern pointer to
- * determine the next character in the pattern, subject to translation of
- * backslash-char and backslash-octal sequences.
- *
- * The character pointer is updated to point at the next pattern character to be
- * processed.
- *
- */
-
- static char
- nextch(patp)
- char **patp;
- {
- register char ch;
- register char chsum;
- register INT count;
-
- ch = *(*patp)++;
- if (ch == '\\') {
- ch = *(*patp)++;
- if (IS_OCTAL(ch)) {
- chsum = 0;
- for (count = 0; count < 3 && IS_OCTAL(ch); count++) {
- chsum *= 8;
- chsum += ch - '0';
- ch = *(*patp)++;
- }
- (*patp)--;
- ch = chsum;
- }
- }
- return (ch);
- }
-
- /*
- * Filename match - here, * matches everything
- */
-
- int
- fmatch(direntry)
- struct direct *direntry;
- {
- char *ptr, *string;
-
- string = direntry->d_name;
-
- if (!strcmp(pattern, "") || !strcmp(pattern, "*.*") || !strcmp(pattern, "*"))
- return (1);
- return (match(string, pattern));
- }
- #else
- /* dir code for MTS under Bell Labs C... */
-
- char *
- dir(filepattern, junk)
- char *filepattern; /* template or NULL */
- INT junk; /* unused on MTS */
- {
- char *malloc(), *index();
- #ifdef USECATSCAN
- fortran catscan(), fileinfo();
-
- struct catname {
- short len;
- char name[257];
- } pattern;
-
- struct catval {
- int maxlen;
- int actlen;
- char name[257];
- } catreturn;
-
- char *i;
- int j, RETCODE;
-
- static int catptr = 0;
- static int catflag = 0x200;
- static int cattype = 1;
- static int patflag = 0;
-
- catreturn.maxlen = 256;
-
- if (patflag) {
- patflag = 0;
- catptr = 0;
- return (NULL);
- }
- if (filepattern) {
- strcpy(pattern.name, filepattern);
- pattern.len = strlen(filepattern);
- if (!index(filepattern, '?'))
- patflag = 1;
- }
- if (patflag) {
- fileinfo(&pattern, &cattype, "CINAME ", &catreturn, _retcode RETCODE);
- catptr = RETCODE ? 0 : 1;
- } else
- catscan(&pattern, &catflag, &cattype, &catreturn, &catptr);
-
- if (!catptr)
- return (NULL);
- else {
- char *k;
-
- k = index(catreturn.name, ' ');
- if (k)
- *k = 0;
- else {
- j = catreturn.actlen;
- catreturn.name[j] = 0;
- }
- k = catreturn.name;
- if (catreturn.name[0] == tmpchr[0])
- k++;
- else if ((k = index(catreturn.name, sepchr[0])))
- k++;
- else
- k = catreturn.name;
- j = strlen(k);
- i = malloc(++j);
- strcpy(i, k);
- return (i);
- }
- #else
- fortran gfinfo();
- static char gfname[24];
- static char pattern[20];
- static int gfdummy[2] = {
- 0, 0
- }, gfflags;
- int i, RETCODE;
- char *j, *k;
-
- if (filepattern) {
- strcpy(pattern, filepattern);
- strcat(pattern, " ");
- for (i = 20; i < 24; i++)
- gfname[i] = '\0';
- if (index(pattern, '?'))
- gfflags = 0x0C;
- else
- gfflags = 0x09;
- } else if (gfflags == 0x09)
- return (NULL);
-
- gfinfo(pattern, gfname, &gfflags, gfdummy, gfdummy, gfdummy, _retcode RETCODE);
- if (RETCODE)
- return (NULL);
- else {
- k = index(gfname, ' ');
- *k = '\0';
- k = gfname;
- if (gfname[0] == tmpchr[0])
- k++;
- else if ((k = index(gfname, sepchr[0])))
- k++;
- else
- k = gfname;
- i = strlen(k);
- j = malloc(++i);
- strcpy(j, k);
- return (j);
- }
- #endif
- }
-
- unlink(path)
- char *path; /* name of file to delete */
- {
- fortran destroy();
- int RETCODE;
-
- char name[258];
-
- strcpy(name, path);
- strcat(name, " ");
- destroy(name, _retcode RETCODE);
- if (RETCODE)
- return (-1);
- else
- return (0);
- }
- #endif
-